package com.jrzh.mvc.convert.sys;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.jrzh.common.exception.ProjectException;
import com.jrzh.common.utils.ReflectUtils;
import com.jrzh.framework.base.convert.BaseConvertI;
import com.jrzh.mvc.model.sys.FileModel;
import com.jrzh.mvc.view.sys.FileView;

@Component("fileConvert")
public class FileConvert implements BaseConvertI<FileModel, FileView> {

	@Override
	public FileModel addConvert(FileView view) throws ProjectException {
		FileModel model = new FileModel();
		ReflectUtils.copySameFieldToTarget(view, model);
		return model;
	}

	@Override
	public FileModel editConvert(FileView view, FileModel model) throws ProjectException {
		ReflectUtils.copySameFieldToTargetFilter(view, model, new String[]{"createBy", "createTime"});
		return model;
	}

	@Override
	public FileView convertToView(FileModel model) throws ProjectException {
		FileView view = new FileView();
		ReflectUtils.copySameFieldToTarget(model, view);
		return view;
	}
	
	public FileModel convertByJsonStr(String json) {
		return JSON.toJavaObject(JSON.parseObject(json), FileModel.class);
	}
	
	public List<FileModel> convertByJsonStrs(String json) {
		List<FileModel> fileList = new ArrayList<FileModel>();
		if(StringUtils.isBlank(json)) return fileList;
		return JSON.parseArray(json, FileModel.class);
	}

}
